<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006~2018 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: liu21st <liu21st@gmail.com>
// +----------------------------------------------------------------------

namespace app;

use app\model\Sms;

class Validate extends \think\Validate
{
    public function __construct()
    {
        parent::__construct();
        // 是否存在
        $this->setTypeMsg('exists', lang(':attribute not exists'));
        $this->extend('exists', fn($value, $rule, array $data = [], string $field = '') => !$this->unique($value, $rule, $data, $field));
        // 不为空时是否存在
        $this->setTypeMsg('existsNotEmpty', lang(':attribute not exists'));
        $this->extend('existsNotEmpty', fn($value, $rule, array $data = [], string $field = '') => empty($value) || !$this->unique($value, $rule, $data, $field));
        // 短信动态码
        $this->setTypeMsg('smscode', lang('smscode error'));
        $this->extend('smscode', function($value/*, $rule, array $data = []*/){
            // $code = Sms::where([['ssid', '=', cookie('PHPSESSID')], ['action', '=', $data['action']]])->order('create_at', 'DESC')->value('code');
            $code = '868686';
            return $code === $value;
        });
    }
    // update 验证场景定义
    public function sceneUpdate()
    {
    	return $this->only(array_merge($this->scene['save'] ?? [], ['id']));
    }
}
